<?php
/**
 * Created By PhpStorm
 * Author: patrick wu
 * Date: 2020/10/7
 * Time: 16:43
 */
namespace App\Http\Controllers\Api;

use Illuminate\Support\Facades\Validator;
use App\Constant\ErrorConstant;
use Illuminate\Http\Request;
use App\Service\Api\LoginService;

class LoginController extends BaseController
{
    protected $service;

    public function __construct(Request $request, LoginService $service)
    {
        parent::__construct($request);
        $this->service  =   $service;
    }

    /**
     * 用户登录
     * @return \Illuminate\Http\JsonResponse
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function checkLogin()
    {
        $validator =    Validator::make($this->request->post(), [
            'code'  =>  'required|string'
        ], [
            'code.required' =>  '登录凭证不能为空',
            'code.string'   =>  '登录凭证必须为字符'
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::API_LOGIN_CHECK_ERROR, $validator->errors()->first());
        }

        $js_code    =   $this->request->post('code');
        $user_unique=   $this->request->input('user_unique', NULL);
        [$flag, $result]    =   $this->service->loginCheck($js_code, $user_unique);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::API_LOGIN_CHECK_ERROR, $result);
        }
    }
}
